﻿using System;
using System.Collections.Generic;
using System.Linq;
using NJabber.Stream.Elements;

namespace NJabber.Client
{
    public class RosterItem : ICloneable
    {
        private string name;
        private JID jid;
        private List<string> groups;

        public RosterItem(string name, JID jid) : this(name, jid, null)
        { }

        public RosterItem(string name, JID jid, IEnumerable<string> groups)
        {
            this.name = name;
            this.jid = jid;
            this.groups = new List<string>();
            if (groups != null) this.groups.AddRange(groups);
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public JID Jid
        {
            get { return jid; }
        }

        public ICollection<string> Groups
        {
            get { return groups; }
        }

        public bool Equals(RosterItem otherItem)
        {
            return name.Equals(otherItem.name) && jid.Equals(otherItem.jid) && Groups.SequenceEqual(otherItem.Groups);
        }

        public override bool Equals(object obj)
        {
            if (!(obj is RosterItem)) return false;
            return Equals((RosterItem) obj);
        }

        public object Clone()
        {
            return new RosterItem(name, jid, groups);
        }

        public override string ToString()
        {
            return string.Format("{0}({1})", name, jid);
        }
        
        public static implicit operator RosterItemElement(RosterItem item)
        {
            return new RosterItemElement(item.Name, item.Jid, item.Groups);
        }
    }
}
